package com.quanyan.assets.repository;

import com.quanyan.assets.emun.AssetsErrorEnum;
import com.quanyan.assets.emun.OpType;
import com.quanyan.assets.emun.StatusType;
import com.quanyan.assets.entity.TbUserEggsAccumulateHistory;
import com.quanyan.assets.entity.TbUserEggsAccumulateHistoryExample;
import com.quanyan.assets.entity.TbUserShellAccumulateHistory;
import com.quanyan.assets.mapper.ETbUserEggsAccumulateHistoryMapper;
import com.quanyan.assets.mapper.TbUserEggsAccumulateHistoryMapper;
import com.quanyan.assets.mapper.TbUserShellAccumulateHistoryMapper;
import com.quanyan.assets.request.ReqUnfreeze;
import com.quanyan.assets.request.ReqUserAccumulateModifyBatch;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by wmb on 2016/6/24.
 */
@Repository
public class EggsAccumulateHistoryRepository {
    @Autowired
    private TbUserEggsAccumulateHistoryMapper tbUserEggsAccumulateHistoryMapper;
    @Autowired
    private ETbUserEggsAccumulateHistoryMapper eTbUserEggsAccumulateHistoryMapper;
    public TbUserEggsAccumulateHistory selectFreeze(ReqUnfreeze reqUnfreeze) {
        TbUserEggsAccumulateHistoryExample tbUserEggsAccumulateHistoryExample=new TbUserEggsAccumulateHistoryExample();
        tbUserEggsAccumulateHistoryExample.createCriteria().andSystemCodeEqualTo(reqUnfreeze.getSystemCode())
                .andBusinessCodeEqualTo(reqUnfreeze.getBusinessCode()).andBusinessNoEqualTo(reqUnfreeze.getBusinessNo())
                .andOpTypeEqualTo(OpType.CONSUME.id.byteValue()).andStatusEqualTo(reqUnfreeze.getStatus());
        List<TbUserEggsAccumulateHistory> tbUserEggsAccumulateHistoryList=tbUserEggsAccumulateHistoryMapper.selectByExample(tbUserEggsAccumulateHistoryExample);
        if(tbUserEggsAccumulateHistoryList.isEmpty()){
            return null;
        }else{
            return tbUserEggsAccumulateHistoryList.get(0);
        }

    }




    public void insertSelective(TbUserEggsAccumulateHistory tbUserEggsAccumulateHistory) {
        tbUserEggsAccumulateHistoryMapper.insertSelective(tbUserEggsAccumulateHistory);
    }


    public TbUserEggsAccumulateHistory selectRechangeBySerialNumber(String serialNumber) {
        TbUserEggsAccumulateHistoryExample tbUserEggsAccumulateHistoryExample=new TbUserEggsAccumulateHistoryExample();
        tbUserEggsAccumulateHistoryExample.createCriteria().andSystemCodeEqualTo(Constants.ASSETS_SYSTEM_CODE)
                .andBusinessCodeEqualTo(Constants.ASSETS_EGGS_RECHANGE_CODE).andSerialNumberEqualTo(serialNumber);
        List<TbUserEggsAccumulateHistory> tbUserEggsAccumulateHistoryList=tbUserEggsAccumulateHistoryMapper.selectByExample(tbUserEggsAccumulateHistoryExample);
        if(tbUserEggsAccumulateHistoryList.isEmpty()){
            return null;
        }else{
            return tbUserEggsAccumulateHistoryList.get(0);
        }
    }

    public int updateRechangeStatus(String serialNumber, byte updateStatus,byte status) {
        return eTbUserEggsAccumulateHistoryMapper.updateStatus(Constants.ASSETS_SYSTEM_CODE,Constants.ASSETS_EGGS_RECHANGE_CODE,serialNumber,updateStatus,status);
    }

    public int updateAccumulateHistory(String businessCode,String serialNumber,Long accumulate) {
        int count=eTbUserEggsAccumulateHistoryMapper.updateAccumulateHistory(businessCode, serialNumber, accumulate);
        return count;
    }

    public void insertAccumulateHistory(TbUserEggsAccumulateHistory tbUserEggsAccumulateHistory) {
        eTbUserEggsAccumulateHistoryMapper.insertAccumulateHistory(tbUserEggsAccumulateHistory);
    }

    public TbUserEggsAccumulateHistory selectByBusinessNo(String systemCode, String businessNo,Byte opType) {
        TbUserEggsAccumulateHistoryExample tbUserEggsAccumulateHistoryExample=new TbUserEggsAccumulateHistoryExample();
        TbUserEggsAccumulateHistoryExample.Criteria criteria=tbUserEggsAccumulateHistoryExample.createCriteria();
        criteria.andSystemCodeEqualTo(systemCode).andBusinessNoEqualTo(businessNo);
        if(null!=opType){
            criteria.andOpTypeEqualTo(opType);
        }
        List<TbUserEggsAccumulateHistory> tbUserEggsAccumulateHistoryList=tbUserEggsAccumulateHistoryMapper.selectByExample(tbUserEggsAccumulateHistoryExample);
        if(tbUserEggsAccumulateHistoryList.isEmpty()){
            return null;
        }else{
            return tbUserEggsAccumulateHistoryList.get(0);
        }
    }

    public int updateRefundSubmit(String systemCode, String businessCode, String businessNo, Long accumulate) {
        return eTbUserEggsAccumulateHistoryMapper.updateRefundSubmit(systemCode, businessCode, businessNo, accumulate);
    }

    public int updateFreezeStatus(ReqUnfreeze reqUnfreeze) {
        return eTbUserEggsAccumulateHistoryMapper.updateFreezeStatus(reqUnfreeze);
    }

    public int updateRefundComplate(String systemCode, String businessCode, String businessNo, Long accumulate, Byte status) {
        return eTbUserEggsAccumulateHistoryMapper.updateRefundComplate(systemCode, businessCode, businessNo, accumulate,status);
    }

    public int selectMaxBatchNo() {
        return eTbUserEggsAccumulateHistoryMapper.selectMaxBatchNo();
    }
}
